package cn.lunatic.pub;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 
 * @Description:封装Map对象，替代Map对象使用
 * @author ganfeng
 * @date 2014-3-6 下午04:10:13
 */
public class DataMap implements Serializable {
	
	private static final long serialVersionUID = 9158104753420028206L;
	
	private SortedMap<Object, Object> map;

	public DataMap() {
		map = new TreeMap<Object, Object>();
	}

	public DataMap(SortedMap<Object, Object> map) {
		this.map = map;
	}

	public void clear() {
		map.clear();
	}

	public boolean containsKey(Object key) {
		if (key == null) {
			return false;
		}
		return map.containsKey(key);
	}

	public Object get(Object key){
		return map.get(key);
	}
	
	public int getInt(Object key){
		Object temp = get(key);
		if (temp == null) {
			throw new NullPointerException();
		}
		if (temp instanceof Integer) {
			return ((Integer) temp).intValue();
		}
		if (temp instanceof Float) {
			return ((Float) temp).intValue();
		}
		if (temp instanceof Long) {
			return ((Long) temp).intValue();
		}
		if (temp instanceof Double) {
			return ((Double) temp).intValue();
		}
		if (temp instanceof String) {
			return Integer.parseInt((String) temp);
		}
		throw new ClassCastException();
	}
	
	public float getFloat(Object key) {
		Object temp = get(key);
		if (temp == null) {
			throw new NullPointerException();
		}
		if (temp instanceof Float) {
			return ((Float) temp).floatValue();
		}
		if (temp instanceof Integer) {
			return ((Integer) temp).floatValue();
		}
		if (temp instanceof Long) {
			return ((Long) temp).floatValue();
		}
		if (temp instanceof Double) {
			return ((Double) temp).floatValue();
		}
		if (temp instanceof String) {
			return Float.parseFloat((String) temp);
		}
		throw new ClassCastException();
	}
	
	public long getLong(Object key) {
		Object temp = get(key);
		if (temp == null) {
			throw new NullPointerException();
		}
		if (temp instanceof Long) {
			return ((Long) temp).longValue();
		}

		if (temp instanceof Float) {
			return ((Float) temp).longValue();
		}
		if (temp instanceof Integer) {
			return ((Integer) temp).longValue();
		}
		if (temp instanceof Double) {
			return ((Double) temp).longValue();
		}
		if (temp instanceof String) {
			return Long.parseLong((String) temp);
		}
		throw new ClassCastException();

	}

	public double getDouble(Object key) {
		Object temp = get(key);
		if (temp == null) {
			throw new NullPointerException();
		}
		if (temp instanceof Double) {
			return ((Double) temp).doubleValue();
		}
		if (temp instanceof Long) {
			return ((Long) temp).doubleValue();
		}

		if (temp instanceof Float) {
			return ((Float) temp).doubleValue();
		}
		if (temp instanceof Integer) {
			return ((Integer) temp).doubleValue();
		}
		if (temp instanceof String) {
			return Double.parseDouble((String) temp);
		}
		throw new ClassCastException();
	}
	
	public String getString(Object key) {
		Object temp = get(key);
		if (temp == null) {
			return null;
		}
		if (temp instanceof Object) {
			return temp.toString();
		}
		return null;
	}

	public int size(Object key) {
		return map.size();
	}

	public String[] getKeys() {
		String[] arr = new String[map.keySet().size()];
		int n = 0;
		Iterator<Object> it = map.keySet().iterator();
		while (it.hasNext()) {
			arr[n++] = (String) it.next();
		}
		return arr;
	}

	public void put(Object key, Object value) {
		map.put(key, value);
	}

	public void set(Object key, Object value) {
		put(key, value);
	}
	
	public void add(Object key, Object value) {
		put(key, value);
	}

	public void addAll(Map<Object, Object> datas) {
		map.putAll(datas);
	}
	
	public Object remove(Object key) {
		return map.remove(key);
	}
	
	public SortedMap<Object, Object> getMap() {
		return map;
	}

	public String[] getArrayString(String[] keys) {
		String[] arr = new String[keys.length];
		for (int i = 0; i < keys.length; i++) {
			arr[i] = getString(keys[i]);
		}
		return arr;
	}
}
